package sa.com.sure.ejtimaa;


import sa.com.sure.ejtimaa.R;
import sa.com.sure.ejtimaa.common.*;
import sa.com.sure.ejtimaa.sync.SyncData;
import android.app.ActionBar;
import android.app.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;


public class MainActivity extends Activity {

	private MeetingListFragment meetingListFragment = null;
	
    /** Called when the activity is first created. */	
    @Override
    public void onCreate(Bundle savedInstanceState) {  
	    try
	    {
	        super.onCreate(savedInstanceState);
	        
	        if(!checkIsAuthenticated()){
	        	
	        	this.finish();
	        	Helper.StartActivity(LoginActivity.class);
	        }
	        
	        setContentView(R.layout.main);

	        ActionBar actionBar = getActionBar();	       
	        
		}
		catch(Exception ex){    		
	    	Helper.HandleError(ex,true);
		}              
    }
        
    @Override
    protected void onStart(){
    	
    	try
	    {
    		super.onStart();
    		
    		meetingListFragment  =(MeetingListFragment) getFragmentManager().findFragmentById(R.id.listFragment);
		}
		catch(Exception ex){    		
	    	Helper.HandleError(ex,true);
		}  
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	try
    	{
	        MenuInflater menuInflater = getMenuInflater();
	        menuInflater.inflate(R.menu.main_options_menu, menu);
	        return true;		
	    }
		catch(Exception ex){    		
	    	Helper.HandleError(ex,true);
		}           
        
    	return false;
    }  
     
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	try
    	{
	    	switch (item.getItemId())
	        {
	        case R.id.imenu_settings:
	        	//Helper.StartActivity(SettingActivity.class);
	        	
	        	OnFileSelectedListener _OnFileSelected = new sa.com.sure.ejtimaa.common.OnFileSelectedListener() {
        		    public void onSelected(String path, String fileName) {
        		      Helper.ShowToast(path + fileName );
        		    }
        		  };
	        	
	        	OpenDialogFile.Instance.ShowDialog(this,_OnFileSelected , null);	        	
	        	
	        	return true;
	        case R.id.imenu_synchronize:
	        	SyncData.Instance.SyncAllData(this,syncHandler);	        	
	        	return true;
	        case R.id.imenu_logout:
	        	
	         	Handler handler = new Handler(){	        		
	                public void handleMessage(Message msg) { 
	                	if(msg.what == AppDialogs.Result_YES)
	                	{        	 	
	        	        	AppPreferences.Instance.setData(AppConstants.Password, "");
	        	        	AppPreferences.Instance.setData(AppConstants.IsAuthenticated, "false");
	        	        	finish();
	        	        	Helper.StartActivity(LoginActivity.class);
	                	}
	                }
	        	};
	        	
	        	AppDialogs.Confirm(this,getString(R.string.AreYouSureForExit), handler);
	        		        		 
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);        	
	        }
    
    	}
 		catch(Exception ex){    		
 	    	Helper.HandleError(ex,true);
 		} 
    	
    	return false;
    }
    
    Handler syncHandler = new Handler() {
        public void handleMessage(Message msg) { 
        	if(msg.what==RESULT_OK)
        	{        		
        		meetingListFragment.RefershDataList();        		
        	}
        	else
        		Helper.ShowToast(R.string.GlobalError);        	
        }
    };
    
    private boolean checkIsAuthenticated(){

    	
    	String isAuthenticated= AppPreferences.Instance.getData(AppConstants.IsAuthenticated);
    	
    	if(isAuthenticated!=null && isAuthenticated.equals("true")){
    		
    		return true;
    	}
    	
    	return false;
    }
    
}